import React from 'react';
import {
  View,
  Text,
  ListView,
  StyleSheet,
  TouchableOpacity,
} from 'react-native';
import PxToDp from '../../../utils/PxToDp';
import FontFamilys from '../../../utils/FontFamilys';
import PreferDetailsList from './PreferDetailsList';


let flagBtn = 0;
class CarColor extends React.Component {
  constructor(props) {
    super(props);

    const { screenProps } = this.props;
    const { car } = screenProps;
    const { data } = car;
    const { colors } = data;

    this.ds = new ListView.DataSource({ rowHasChanged: (r1, r2) => r1 !== r2 });
    this.state = {
      colors,
      deftBg: '#418cff',
      whiteBg: '#fff',
    };
  }

  changeBtn(flag) {
    if (flag) {
      this.setState({ deftBg: '#fff' });
      this.setState({ whiteBg: '#418cff' });
      flagBtn = 1;
    } else {
      this.setState({ deftBg: '#418cff' });
      this.setState({ whiteBg: '#fff' });
      flagBtn = 0;
    }
  }

  render() {
    return (
      <View style={[styles.flex, styles.listBg]}>

        <View style={styles.lineView}>
          <View style={styles.line} />
        </View>
        <View style={[styles.row, styles.between]}>
          <View style={[styles.row, styles.paddingLR]}>
            <TouchableOpacity
              style={[styles.btn, { backgroundColor: this.state.deftBg }]}
              onPress={() => this.changeBtn(0)}
            >
              <Text style={[{ color: this.state.whiteBg }, styles.title]}>观看时长</Text>
            </TouchableOpacity>

            <TouchableOpacity
              style={[styles.btn, styles.countBtn, { backgroundColor: this.state.whiteBg }]}
              onPress={() => this.changeBtn(1)}
            >
              <Text style={[{ color: this.state.deftBg }, styles.title]}>观看次数</Text>
            </TouchableOpacity>
          </View>
        </View>
        <ListView
          dataSource={this.ds.cloneWithRows(this.state.colors)}
          renderRow={color =>
            <PreferDetailsList
              name={color[1].name}
              duration={color[1].mins}
              rate={color[1].mins_rate}
              times={color[1].count}
              color={color[1].color_value}
              type={'showColor'}
              flagBtn={flagBtn}
            />
          }
        />

      </View>
    );
  }
}

const styles = StyleSheet.create({
  flex: {
    flex: 1,
  },
  row: {
    flexDirection: 'row',
  },
  listBg: {
    backgroundColor: '#FFF',
  },
  between: {
    justifyContent: 'space-between',
  },
  paddingLR: {
    paddingLeft: PxToDp(20),
  },
  title: {
    fontSize: PxToDp(14),
    fontFamily: FontFamilys(),
  },
  btn: {
    height: PxToDp(28),
    width: PxToDp(89),
    alignItems: 'center',
    justifyContent: 'center',
    borderRadius: PxToDp(100),
    marginTop: PxToDp(24),
    marginBottom: PxToDp(10),
    borderWidth: PxToDp(1),
    borderStyle: 'solid',
    borderColor: '#418cff',
  },
  countBtn: {
    marginLeft: PxToDp(10),
  },
  line: {
    height: PxToDp(0.5),
    width: '100%',
    backgroundColor: '#dadada',
  },
  lineView: {
    paddingLeft: PxToDp(20),
    paddingRight: PxToDp(20),
    paddingTop: PxToDp(6),
  },
});

export default CarColor;

